package pixelradar.gui;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import pixelradar.map.Marker;
import pixelradar.map.MarkerManager;
import pixelradar.util.Utils;

public class MwGuiMarkerListOverlay extends MwGuiSlot {
    public static int listWidth = 95;
    public static int ListY = 10;
    private final GuiScreen parentScreen;
    private final MarkerManager markerManager;
    private int height;

    public MwGuiMarkerListOverlay(GuiScreen parentScreen, MarkerManager markerManager) {
        super(Minecraft.getMinecraft(), listWidth, parentScreen.height - 20, ListY, 10 + parentScreen.height - 20, parentScreen.width - 110);
        this.height = parentScreen.height - 20;
        this.parentScreen = parentScreen;
        this.markerManager = markerManager;
    }

    protected int getSlotHeight(int index) {
        String str = Utils.stringArrayToString(this.getLabelString(index));
        int height = this.mc.fontRenderer.getWordWrappedHeight(str, listWidth - 6);
        height += this.spacingY * 2;
        return height;
    }

    protected String[] getLabelString(int index) {
        Marker m = (Marker) this.markerManager.visibleMarkerList.get(index);
        return new String[]{m.name, String.format("(%d, %d, %d)", m.x, m.y, m.z)};
    }

    protected int getSize() {
        return this.markerManager.visibleMarkerList.size();
    }

    protected void elementClicked(int slotIndex, boolean isDoubleClick, int mouseX, int mouseY, int mouseButton) {
        this.markerManager.selectedMarker = (Marker) this.markerManager.visibleMarkerList.get(slotIndex);
        if (mouseButton == 1 && this.parentScreen instanceof MwGui) {
            ((MwGui) this.parentScreen).openMarkerGui(this.markerManager.selectedMarker, mouseX, mouseY);
        }

        if (mouseButton == 0 && isDoubleClick && this.parentScreen instanceof MwGui) {
            ((MwGui) this.parentScreen).centerOnSelectedMarker();
        }

    }

    protected boolean isSelected(int slotIndex) {
        return this.markerManager.selectedMarker == this.markerManager.visibleMarkerList.get(slotIndex);
    }

    protected void drawBackground() {
    }

    protected void drawSlot(int entryID, int x, int y, int slotHeight, int mouseXIn, int mouseYIn) {
        MwGuiLabel label = new MwGuiLabel(this.getLabelString(entryID), (String[]) null, x, y, Boolean.FALSE, Boolean.FALSE, listWidth, this.height);
        label.draw();
    }

    public void setDimensions(int widthIn, int heightIn, int topIn, int bottomIn, int left) {
        this.height = this.parentScreen.height - 20;
        super.setDimensions(widthIn, heightIn, topIn, bottomIn, left);
    }
}
